package de.openlaboratory.elib.server;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.utilities.ServletUtils;

public class RequestHelper
{
	private String commandString;
	private String operationString;
	private Command command;

	public RequestHelper(HttpServletRequest request)
	{
		commandString = ServletUtils.getPage(request);
		
		extractOperationFromAttributes(request);
		
		if ((commandString != null) && (operationString != null))
		{
			setCommand(commandString, operationString);
		}
		else
		{
			this.command = new Command(commandString);
		}
		
	}
	
	private void extractOperationFromAttributes(HttpServletRequest request)
	{
		Enumeration attrNames = request.getAttributeNames();
		
		while(attrNames.hasMoreElements())
		{
			String attr = attrNames.nextElement().toString();
			if (attr == null)
				break;
			
			if (attr.toUpperCase().equals("COMMAND"))
			{
				this.commandString = (String) request.getAttribute(attr);
			}
			else if (attr.toUpperCase().equals("ACTION"))
			{
				this.operationString = (String) request.getAttribute(attr);
			}
		}
	}
	
	private void setCommand(String cmd, String op)
	{
		this.command = new Command(cmd, op);
	}

	public Command getCommand()
	{
		return command;
	}

}
